home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / c / jpl_c.zip / TAN.C < prev    next >
Text File  |  1986-05-18  |  3KB  |  108 lines

  1. /* 1.1  07-08-85 */
  2. /************************************************************************
  3.  *            Robert C. Tausworthe                *
  4.  *            Jet Propulsion Laboratory            *
  5.  *            Pasadena, CA 91009        1985        *
  6.  ************************************************************************
  7.  *    Programmmed using the algorithms given in:
  8.  *
  9.  *    Coty, William J., Jr., and Waite, William, SOFTWARE MANUAL FOR
  10.  *    THE ELEMENTARY FUNCTIONS, Prentice-Hall Series in Computational
  11.  *    Mathematics, Prentice-Hall, Inc., Inglewood Cliffs, NJ, 1980,
  12.  *    pp. 150-173.
  13.  *
  14.  *----------------------------------------------------------------------*/
  15.  
  16. #include "defs.h"
  17. #include "stdtyp.h"
  18. #include "errno.h"
  19. #include "mathtyp.h"
  20. #include "mathcons.h"
  21.  
  22. /*----------------------------------------------------------------------*/
  23.  
  24. #define C1    1.57080078125            /* C1 - C2 = PIover2    */
  25. #define C2    4.454455103380768678308e-6
  26.  
  27. #define P0     1.0
  28. #define P1     0.13338350006421960681e+0
  29. #define P2     0.34248878235890589960e-2
  30. #define P3     0.17861707342254426711e-4
  31. #define P(g)    (((-P3*g+P2)*g-P1)*g)
  32.  
  33. #define Q0     1.0
  34. #define Q1     0.46671683339755294240e+0
  35. #define Q2     0.25663832289440112864e-1
  36. #define Q3     0.31181531907010027307e-3
  37. #define Q4     0.49819433993786512270e-6
  38. #define Q(g)    (((((Q4*g-Q3)*g+Q2)*g-Q1)*g+0.5)+0.5)
  39.  
  40. LOCAL double tancot();
  41.  
  42. /*\p*********************************************************************/
  43.     double
  44. tan(x)            /* return trigonometric tangent of x        */
  45.  
  46. /*----------------------------------------------------------------------*/
  47. double x;
  48. {
  49.     return tancot(x, ABS(x), 0);
  50. }
  51.  
  52. /************************************************************************/
  53.     double
  54. cotan(x)        /* returns trigonometric cotangent of x        */
  55.  
  56. /*----------------------------------------------------------------------*/
  57. double x;
  58. {
  59.     double y;
  60.     
  61.     y = ABS(x);
  62.     if (y < SYMLEAST)
  63.     {    errno = ERANGE;
  64.         if (x < 0.0)
  65.             return -INFINITY;
  66.         else
  67.             return INFINITY;
  68.     }
  69.     return tancot(x, y, 1);
  70. }
  71.  
  72. /*\p*********************************************************************/
  73.     LOCAL double
  74. tancot(x, y, flag)        /* process both tan and cot functions    */
  75.  
  76. /*----------------------------------------------------------------------*/
  77. double x,y;
  78. BOOL flag;
  79. {
  80.     double f, g, x1, x2, xn;
  81.     double xnum, xden;
  82.     
  83.     if (y > MAXANGLE)
  84.     {    errno = ERANGE;
  85.         return 0.0;
  86.     }
  87.     xn = round(TWOoverPI * x);
  88.     x1 = fint(x);
  89.     f = ((x1 - xn * C1) + (x - x1)) + xn * C2;
  90.     if (ABS(f) < FADEOUT)
  91.     {    xnum = f;
  92.         xden = 1.0;
  93.     }
  94.     else
  95.     {    g = f * f;
  96.         xnum = P(g) * f + f;
  97.         xden = Q(g);
  98.     }
  99.     if ((int) xn & 1)
  100.     {    flag = !flag;    /* invert tan-cotan logic    */
  101.         xnum = -xnum;    /* Cases A and C, xn odd    */
  102.     }
  103.     if (flag)
  104.         return (xden / xnum);    /* inverted function    */
  105.     else        
  106.         return (xnum / xden);    /* normal function    */
  107. }
  108.